package cz.muni.fi.pa165.sme.web.panels;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

/**
 * @author jirankova
 */
public abstract class ClickablePropertyColumn<T> extends AbstractColumn<T> {
	private final String property;

	public ClickablePropertyColumn(final IModel<String> displayModel, final String property) {
		super(displayModel, null);
		this.property = property;
	}

	public void populateItem(final Item<ICellPopulator<T>> cellItem, final String componentId,
			final IModel<T> rowModel) {
		cellItem.add(new LinkPanel(componentId, rowModel, new PropertyModel<Object>(rowModel, property)));
	}

	protected abstract void onClick(IModel<T> rowModel);

	private class LinkPanel extends Panel {
		public LinkPanel(final String id, final IModel<T> rowModel, final IModel<?> labelModel) {
			super(id);
			final Link<T> link = new Link<T>("link", rowModel) {
				@Override
				public void onClick() {
					ClickablePropertyColumn.this.onClick(getModel()); //to let the user know which row was clicked
				}
			};
			add(link);
			link.add(new Label("label", labelModel));
		}
	}
}
